package cn.hzking.gjmall.net.base;

import android.accounts.NetworkErrorException;
import android.app.Activity;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;

import cn.hzking.gjmall.net.loading.loadingview.GjLoadingUtils;
import cn.hzking.gjmall.utils.Logger;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2018/1/20.
 */

public abstract class BaseObserver<T extends BaseEntity> implements Observer<T> {

    protected Activity mActivity;
    private boolean isShowDialog;//是否要显示对话框
    private boolean isCancel; //按返回键能否取消对话框

    public BaseObserver(Activity cxt,boolean isShowDialog,boolean isCancel) {
        this.mActivity = cxt;
        this.isShowDialog = isShowDialog;
        this.isCancel = isCancel;
    }
    public BaseObserver(Activity cxt,boolean isShowDialog) {
        this.mActivity = cxt;
        this.isShowDialog = isShowDialog;
        this.isCancel = true;
    }

    @Override
    public void onSubscribe(Disposable d) {
        onRequestStart();

    }

    @Override
    public void onNext(T tBaseEntity) {
        onRequestEnd();
        if (tBaseEntity.isSuccess()) {
            try {
                onSuccees(tBaseEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                onCodeError(tBaseEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onError(Throwable e) {
//        Log.w(TAG, "onError: ", );这里可以打印错误信息
        Logger.e("错误");
        try {
            if (e instanceof TimeoutException
                    || e instanceof NetworkErrorException
                    || e instanceof UnknownHostException || e instanceof SocketTimeoutException) {
                onFailure(e, true);
            } else {
                onFailure(e, false);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        onRequestEnd();
    }

    @Override
    public void onComplete() {
    }

    /**
     * 返回成功
     *
     * @param t
     * @throws Exception
     */
    protected abstract void onSuccees(T t) throws Exception;

    /**
     * 返回成功了,但是code错误
     *
     * @param t
     * @throws Exception
     */
    protected void onCodeError(T t) throws Exception {
    }

    /**
     * 返回失败
     *
     * @param e
     * @param isNetWorkError 是否是网络错误
     * @throws Exception
     */
    protected abstract void onFailure(Throwable e, boolean isNetWorkError) throws Exception;

    protected void onRequestStart() {
    if(isShowDialog && mActivity != null) {
            showProgressDialog();
        }
    }

    protected void onRequestEnd() {
        if ( mActivity == null || mActivity.isFinishing())
            return;
        try {
            closeProgressDialog();

        } catch (Exception e) {
        }

    }

    private void showProgressDialog() {
        GjLoadingUtils.showLoading(mActivity,isCancel);
        //  loadingDialog = LoadingDialogUtils.createLoadingDialog(mActivity, "", isCancel);
       // loadingDialog.show();
    }

    private void closeProgressDialog() {
        /*if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }*/
        GjLoadingUtils.stopLoading();
    }
}

